package com.hibernate.hrm.dao;

import com.hibernate.hrm.entity.ContractAttachment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ContractAttachmentRepository extends JpaRepository<ContractAttachment, Integer> {
    List<ContractAttachment> findByContract_ContractId(Integer contractId);
    @Modifying
    @Query("DELETE FROM ContractAttachment ca WHERE ca.attachmentId = :attachmentId AND ca.contract.contractId = :contractId")
    int deleteAttachment(@Param("contractId") Integer contractId, @Param("attachmentId") Integer attachmentId);

    @Query("SELECT ca FROM ContractAttachment ca WHERE ca.contract.contractId = :contractId ORDER BY ca.createTime DESC")
    List<ContractAttachment> findAllByContractIdOrderByCreateTimeDesc(@Param("contractId") Integer contractId);
}
